package com.example.util.sql;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

public class TableUtils {

    public static List<String> getTables(String tableSuffix,Integer count){
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < count; i++) {
            String tableName = tableSuffix + i;
            list.add(tableName);
        }
        return list;
    }

    public static String getUnionAllTable(List<String> tables){
        return tables.stream()
                .map(tableName ->
                        String.format("SELECT * FROM %s ",
                                tableName))
                .collect(Collectors.joining(" UNION ALL "));
    }

    public static Map<String,String> nodeTableMap(String node,String table,Integer count){
        Map<String,String> map = new HashMap<String,String>();
        for (int i = 0; i < count; i++) {
            String tableName = table + i;
            String nodeName = node + i;
            map.put(nodeName,tableName);
        }
        return map;
    }

    public static List<String> getNodes(String node,Integer count){
        List<String> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            String nodeName = node + i;
            list.add(nodeName);
        }
        return list;
    }

    public static void main(String[] args) throws Exception {
        example2();
    }

    // 创建固定大小的线程池
    private static final ExecutorService executor = Executors.newFixedThreadPool(5);
    /**
     * 示例2：使用CompletableFuture（推荐方式）
     */
    public static void example2() throws Exception {
        System.out.println("\n=== 示例2：CompletableFuture方式 ===");

        List<CompletableFuture<String>> futures = new ArrayList<>();
        Random random = new Random();

        // 提交10个任务
        for (int i = 1; i <= 10; i++) {
            final int taskId = i;
            CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                try {
                    System.out.println(Thread.currentThread().getName() + " 开始执行任务 " + taskId);
                    long time=random.nextInt(1000) + 500;
                    Thread.sleep(time);
                    return "任务-" + taskId + "-结果 执行"+time;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }, executor);

            futures.add(future);
        }

        // 等待所有任务完成并收集结果
        CompletableFuture<Void> allFutures = CompletableFuture.allOf(
                futures.toArray(new CompletableFuture[0])
        );

        CompletableFuture<List<String>> allResults = allFutures.thenApply(v ->
                futures.stream()
                        .map(CompletableFuture::join).collect(Collectors.toList())
        );

        // 获取最终结果
        List<String> results = allResults.get();

        System.out.println("\n=== 汇总结果 ===");
        results.forEach(System.out::println);
        System.out.println("总共完成 " + results.size() + " 个任务");
    }

}
